/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flowable.app.rest.history;

//import org.flowable.app.model.runtime.SaveFormRepresentation;

import org.flowable.app.domain.editor.Flow;
import org.flowable.app.domain.editor.ModelInformation;
import org.flowable.app.repository.editor.CusFlowRepository;
import org.flowable.app.repository.editor.ModelRelationRepository;
import org.flowable.app.service.runtime.FlowableTaskFormService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.impl.HistoryServiceImpl;
import org.flowable.engine.impl.TaskServiceImpl;
import org.flowable.engine.repository.Deployment;
import org.flowable.form.model.FormModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Joram Barrez
 */
@RestController
@RequestMapping("/rest/my-history")
public class CusProcessHistoryResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(CusProcessHistoryResource.class);
    @Autowired
    protected FlowableTaskFormService taskFormService;
    @Autowired
    protected TaskServiceImpl taskService;
    @Autowired
    protected HistoryServiceImpl historyService;

    @Autowired
    protected RepositoryService repositoryService;

    @Autowired
    private CusFlowRepository cusFlowRepository;


    @RequestMapping(value = "/{deploymentKey}", method = RequestMethod.GET, produces = "application/json")
    public List<HistoricProcessInstance> getHistoricProcessInstance(@PathVariable String deploymentKey) {
        List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentKey(deploymentKey).list();

        List<String> deploymentIds = new ArrayList<String>();
        for (Deployment deployment : deployments) {
            deploymentIds.add(deployment.getId());
        }
        List<HistoricProcessInstance> historicProcessInstancesList= historyService.createHistoricProcessInstanceQuery().deploymentIdIn(deploymentIds).list();
        return historicProcessInstancesList;
    }



}
